package com.pingan.cloud.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.pingan.cloud.dto.User;

@RestController
public class UserController {

	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private LoadBalancerClient lbCli;
	
	@GetMapping("/movie/{id}")
	public User findById(@PathVariable Integer id) {
		return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
	}
	
	@GetMapping("/test")
	public String test() {
		ServiceInstance si = this.lbCli.choose("microservice-provider-user");
		System.out.println(si.getServiceId() + ":" + si.getHost() + ":" + si.getPort() );
		
		ServiceInstance si2 = this.lbCli.choose("microservice-provider-user2");
		System.out.println(si2.getServiceId() + ":" + si2.getHost() + ":" + si2.getPort() );
		return "1";
	}
}
